/*
 * http://www.digione.cn
 */
package com.jqd.examples.hibernate.tutorial01;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Proxy;

/**
 * TODO
 * 
 * @author liaozc@digione.cn
 * @date 2012-8-14
 * @since 1.0
 */
@Entity
//使用动态更新，hibernate 产生的 sql 只会更新那些有变动的字段，不是所有的都更新
@DynamicUpdate
@DynamicInsert
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Access(AccessType.PROPERTY)
@Proxy
public class ProductMock implements Serializable {
	
	private static final long serialVersionUID = -4693960655093050659L;
	
	private Long id;
	
	private String name;
	
	private Date createDate;
	
	private Timestamp destoryDate;
	
	// no getter and setter property
	// 但是访问类型使用 field，所以数据库中也会映射该字段
	// 如果不指定访问类型为 field，则采用 class 级别定义的访问类型 property 类型，则数据库中不会映射该字段（因为没有 get/set 方法）
	@Access(AccessType.FIELD)
	@Column(name = "counts")
	private Integer count;
	
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}
	
	
	public void setId(Long id) {
		this.id = id;
	}
	
	
	@Column(length = 20, nullable = false, updatable = false)
	public String getName() {
		return name;
	}
	
	
	public void setName(String name) {
		this.name = name;
	}
	
	
	@Column(length = 20)
	// ����ָ��������������ݿ��е�ӳ�����ͣ���ѡֵ�ο� TemporalType.java
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}
	
	
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	
	@Column(length = 20)
	public Timestamp getDestoryDate() {
		return destoryDate;
	}
	
	
	public void setDestoryDate(Timestamp destoryDate) {
		this.destoryDate = destoryDate;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductMock other = (ProductMock) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		}
		else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", createDate=" + createDate + ", count="
				+ count + "]";
	}
	
}
